package com.zj.fx.controller.month;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zj.fx.common.base.BaseController;
import com.zj.fx.common.domain.AjaxResult;
import com.zj.fx.common.domain.ResultTable;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.model.month.*;
import com.zj.fx.service.month.MonthAuditDtlService;
import com.zj.fx.service.month.MonthEvaluationService;
import com.zj.fx.service.month.MonthSupplytaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 月度审核部门完成情况明细Controller
 *
 * @author SQL
 * @ClassName: MonthAuditDtlController
 * @date 2023-06-26 11:07:35
 */
@Api(value = "月度审核部门完成情况明细")
@Controller
@RequestMapping("/MonthAuditDtlController")
public class MonthAuditDtlController extends BaseController {

    private String prefix = "month/monthAuditDtl";

    @Autowired
    private MonthAuditDtlService monthAuditDtlService;

    @Autowired
    private MonthSupplytaskService monthSupplytaskService;


    /**
     * 月度审核部门完成情况明细页面展示
     *
     * @param model
     * @return String
     * @author SQL
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("gen:monthAuditDtl:view")
    public String view(ModelMap model) {
        return prefix + "/list";
    }

    /**
     * list集合
     *
     * @param tablepar
     * @param
     * @return
     */
    //@Log(title = "月度审核部门完成情况明细", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list")
    @RequiresPermissions("gen:monthAuditDtl:list")
    @ResponseBody
    public ResultTable list(Tablepar tablepar, MonthAuditDtl monthAuditDtl) {
        PageInfo<MonthAuditDtl> page = monthAuditDtlService.list(tablepar, monthAuditDtl);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 新增跳转
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }

    /**
     * 新增保存
     *
     * @param
     * @return
     */
    //@Log(title = "月度审核部门完成情况明细新增", action = "111")
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    @RequiresPermissions("gen:monthAuditDtl:add")
    @ResponseBody
    public AjaxResult add(MonthAuditDtl monthAuditDtl) {
        int b = monthAuditDtlService.insertSelective(monthAuditDtl);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 月度审核部门完成情况明细删除
     *
     * @param ids
     * @return
     */
    //@Log(title = "月度审核部门完成情况明细删除", action = "111")
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/remove")
    @RequiresPermissions("gen:monthAuditDtl:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = monthAuditDtlService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }


    /**
     * 修改跳转
     *
     * @param id
     * @param
     * @return
     */
    @ApiOperation(value = "修改跳转", notes = "修改跳转")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map) {
        map.put("MonthAuditDtl", monthAuditDtlService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    //@Log(title = "月度审核部门完成情况明细修改", action = "111")
    @ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:monthAuditDtl:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MonthAuditDtl monthAuditDtl) {
        return toAjax(monthAuditDtlService.updateByPrimaryKeySelective(monthAuditDtl));
    }

    /**
     * list集合
     *
     * @param tablepar
     * @param
     * @return
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list4AuditPlan/{evaluationId}")
    @ResponseBody
    public ResultTable list4AuditPlan(@PathVariable("evaluationId") String evaluationId, Tablepar tablepar, MonthSupplytask monthSupplytask) {
        MonthSupplytaskExample monthSupplytaskExample = new MonthSupplytaskExample();
        MonthSupplytaskExample.Criteria criteria = monthSupplytaskExample.createCriteria();
        criteria.andFkEvaluationIdEqualTo(evaluationId);
        if (Strings.isNotBlank(monthSupplytask.getSupplyName())){
            criteria.andLikeQuery(monthSupplytask);
        }
        List<MonthSupplytask> monthSupplytaskList = monthSupplytaskService.selectByExample(monthSupplytaskExample);
        List<MonthAuditDtl> monthAuditDtls = new ArrayList<>();
        for (MonthSupplytask supplytask : monthSupplytaskList) {
            MonthAuditDtlExample monthAuditDtlExample = new MonthAuditDtlExample();
            MonthAuditDtlExample.Criteria criteria1 = monthAuditDtlExample.createCriteria();
            criteria1.andFkTaskIdEqualTo(supplytask.getId());
            List<MonthAuditDtl> monthAuditDtls1 = monthAuditDtlService.selectByExample(monthAuditDtlExample);

//            MonthAuditDtl monthAuditDtl1 = new MonthAuditDtl();
//            monthAuditDtl1.setFkTaskId(supplytask.getId());
//            List<MonthAuditDtl> monthAuditDtls1 = monthAuditDtlService.selectByFkTaskId(monthAuditDtl1);

            MonthAuditDtl auditDtl = new MonthAuditDtl();

            if (monthAuditDtls1.size() == 0) {
                auditDtl.setYingxiaoState(MonthConstant.NOT);
                auditDtl.setYunjianState(MonthConstant.NOT);
                auditDtl.setAnjianState(MonthConstant.NOT);
                auditDtl.setDangjianState(MonthConstant.NOT);
                auditDtl.setFazhanState(MonthConstant.NOT);
                auditDtl.setHouqinState(MonthConstant.NOT);
                auditDtl.setHulianwangState(MonthConstant.NOT);
                auditDtl.setJiweiState(MonthConstant.NOT);
                auditDtl.setRenziState(MonthConstant.NOT);
                auditDtl.setGonghuiState(MonthConstant.NOT);
                auditDtl.setXintongState(MonthConstant.NOT);
                auditDtl.setBangongshiState(MonthConstant.NOT);
            } else {
                for (MonthAuditDtl monthAuditDtl : monthAuditDtls1) {
                    if (MonthConstant.DEPT_ID_YINGXIAO.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setYingxiaoState(MonthConstant.IS);
                        } else {
                            auditDtl.setYingxiaoState(MonthConstant.NOT);
                        }
                    }
                    if (MonthConstant.DEPT_ID_YUNJIAN.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setYunjianState(MonthConstant.IS);
                        } else {
                            auditDtl.setYunjianState(MonthConstant.NOT);
                        }
                    }
                    if (MonthConstant.DEPT_ID_ANJIAN.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setAnjianState(MonthConstant.IS);
                        } else {
                            auditDtl.setAnjianState(MonthConstant.NOT);
                        }
                    }
                    if (MonthConstant.DEPT_ID_DANGJIAN.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setDangjianState(MonthConstant.IS);
                        } else {
                            auditDtl.setDangjianState(MonthConstant.NOT);
                        }
                    }
//                    if (MonthConstant.DEPT_ID_FAZHAN.equals(monthAuditDtl.getDeptId())) {
//                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
//                            auditDtl.setFazhanState(MonthConstant.IS);
//                        } else {
//                            auditDtl.setFazhanState(MonthConstant.NOT);
//                        }
//                    }
                    if (MonthConstant.DEPT_ID_HOUQIN.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setHouqinState(MonthConstant.IS);
                        } else {
                            auditDtl.setHouqinState(MonthConstant.NOT);
                        }
                    }
                    if (MonthConstant.DEPT_ID_HULIANWANG.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setHulianwangState(MonthConstant.IS);
                        } else {
                            auditDtl.setHulianwangState(MonthConstant.NOT);
                        }
                    }
                    if (MonthConstant.DEPT_ID_JIWEI.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setJiweiState(MonthConstant.IS);
                        } else {
                            auditDtl.setJiweiState(MonthConstant.NOT);
                        }
                    }
                    if (MonthConstant.DEPT_ID_RENZI.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setRenziState(MonthConstant.IS);
                        } else {
                            auditDtl.setRenziState(MonthConstant.NOT);
                        }
                    }

                    if (MonthConstant.DEPT_ID_GONGHUI.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setGonghuiState(MonthConstant.IS);
                        } else {
                            auditDtl.setGonghuiState(MonthConstant.NOT);
                        }
                    }

                    if (MonthConstant.DEPT_ID_XINTONG.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setXintongState(MonthConstant.IS);
                        } else {
                            auditDtl.setXintongState(MonthConstant.NOT);
                        }
                    }

                    if (MonthConstant.DEPT_ID_BANGONGSHI.equals(monthAuditDtl.getDeptId())) {
                        if (MonthConstant.IS.equals(monthAuditDtl.getTaskState())) {
                            auditDtl.setBangongshiState(MonthConstant.IS);
                        } else {
                            auditDtl.setBangongshiState(MonthConstant.NOT);
                        }
                    }
                }
            }
            auditDtl.setSupplyName(supplytask.getSupplyName());
            auditDtl.setTaskMonth(supplytask.getTaskMonth());
            auditDtl.setTaskState(supplytask.getTaskState());
            monthAuditDtls.add(auditDtl);

        }
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        PageInfo<MonthAuditDtl> pageInfo = new PageInfo<MonthAuditDtl>(monthAuditDtls);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }
}
